//package java_encrypt;
//
//import cn.hutool.core.codec.Base64Decoder;
//import cn.hutool.core.codec.Base64Encoder;
//
//import javax.crypto.Cipher;
//import java.security.*;
//import java.security.spec.PKCS8EncodedKeySpec;
//import java.security.spec.RSAPublicKeySpec;
//import java.security.spec.X509EncodedKeySpec;
//import java.util.Base64;
//
///**
// * 摘自：https://blog.csdn.net/qq_18870023/article/details/52596808
// */
//public class demo_04_RSA {
//    public static String data="15908085458";
//
//    public static void main(String[] args) throws Exception {
////        KeyPair keyPair=makeKey(1024);
////        System.out.println("加密前："+data);
////
////        //获取公钥，并以base64格式打印出来
////        PublicKey publicKey=keyPair.getPublic();
////        System.out.println("公钥："+new String(Base64.getEncoder().encode(publicKey.getEncoded())));
////
////        //获取私钥，并以base64格式打印出来
////        PrivateKey privateKey=keyPair.getPrivate();
////        System.out.println("私钥："+new String(Base64.getEncoder().encode(privateKey.getEncoded())));
////
////        //公钥加密
////        byte[] encryptedBytes=encrypt(data.getBytes(), publicKey);
////        System.out.println("加密后："+new String(encryptedBytes));
////
////        //私钥解密
////        byte[] decryptedBytes=decrypt(encryptedBytes, privateKey);
////        System.out.println("解密后："+new String(decryptedBytes));
//
//
//        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCP3wZ9QeuGM/9UnwGULzfxKXhYP/PX5MPAyEiNDmHCQAi+DSqpd1+z5LuHdKkU3qGqCkgwGaOnMGf4LeS/GoUm1HuI4QWJdNUOMAW1VsKM4qotmpzXWuXG/ed9k+Hg3WW0HI9wvbCZsJ9H6XkrFxipf26qTkVnUM+9+47lMy+ybwIDAQAB";
//        String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAI/fBn1B64Yz/1SfAZQvN/EpeFg/89fkw8DISI0OYcJACL4NKql3X7Pku4d0qRTeoaoKSDAZo6cwZ/gt5L8ahSbUe4jhBYl01Q4wBbVWwoziqi2anNda5cb9532T4eDdZbQcj3C9sJmwn0fpeSsXGKl/bqpORWdQz737juUzL7JvAgMBAAECgYBjhkze6XMca2v+BeD5uZBha+asZdExUDd6bRKDQaeu0M07a7BPexzhPSHdGn68ySXckzBM1e+4AGxr6pOK/7WLkyqZ95ZZobCQVN71QxW6/EA+ZNdYUR/+6vAqktXZBio0RDBVG1HAKTRWphYT4357KNSg27EEZqmiWbnMHWTtgQJBANsB+meG/M4BIJfmgwk1XECIcmrxG2LDevToc/AGosJUJ/VpWSFLGrmwNMlcqEXk41Z7M6hRWnSL2DQJvPW1JhkCQQCoLBmB2YXI94N+1BsG4f37d6MI4Rm/WUX3HNI9ceuvoIJE2OIDoAWFS9zQFPSxBm1aJYGrOmKltNOOK9DJyF3HAkBe3z6ANoOxId/iraK6DHmIIxOdrBC2+gN7sG743YcIM7iToS1RNN3A7soPKKpkjMDGp9w63/MVZbck48sNjYmRAkB1J1uRmM5ZZQwm024MimzbvMlHy0Cbi35xGpPfmDf+/ksh2RECQ3z1Ehc+ogqgNAED1rMRbLgCBgmGtkFaAIpLAkBreHUxIOWPDXNM9Jg4ROo2AtXsCDHONASJEVYA2ngoYPGfYbX+QwEqBm1mtc0XtU9K4crdNIvakAWuo7VLQ3Cd";
//
//        //公钥加密
//        byte[] encryptedBytes=encrypt(data.getBytes(), publicKey);
//        System.out.println("加密后："+ Base64Encoder.encode(encryptedBytes));
//
//        //私钥解密
//        byte[] decryptedBytes=decrypt(encryptedBytes, privateKey);
//        System.out.println("解密后："+new String(decryptedBytes));
//    }
//
//    //生成密钥对
//    public static KeyPair makeKey(int keyLength) throws Exception{
//        KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA"); //
//        keyPairGenerator.initialize(keyLength); // 密钥
//        return keyPairGenerator.generateKeyPair();
//    }
//
//    //公钥加密
//    public static byte[] encrypt(byte[] content, Key key) throws Exception{
//        Cipher cipher= Cipher.getInstance("RSA");
//        cipher.init(Cipher.ENCRYPT_MODE, key);
//        return cipher.doFinal(content);
//    }
//    //公钥加密
//    public static byte[] encrypt(byte[] content, String key) throws Exception{
//        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64Decoder.decode(key));
//        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//        PublicKey publicKey = keyFactory.generatePublic(keySpec);
//
//        Cipher cipher= Cipher.getInstance("RSA");
//        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//        return cipher.doFinal(content);
//    }
//
//    //私钥解密
//    public static byte[] decrypt(byte[] content, String key) throws Exception{
//
//        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64Decoder.decode(key));
//        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
//
//        Cipher cipher=Cipher.getInstance("RSA");
//        cipher.init(Cipher.DECRYPT_MODE, privateKey);
//        return cipher.doFinal(content);
//    }
//
//}